package Meta

import (
	"fmt"
	"db2s/ETL"
	"db2s/global"
	"db2s/parDef"
	"db2s/ref"
)

func (ma Meta) columnEr(drive string) (ter ColumnEr, err error) {
	if ter, err = NewMetaColumn(drive); err != nil {
		return
	}
	return
}
func (ma Meta) columnMeta() (meta any, err error) {
	var ter ColumnEr
	if meta, err = parDef.CallMethod(ma.Drive, ma.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = ma.columnEr(v); err != nil {
			return
		}
		if result, err = ter.Meta(parDef.CtxBackground(parDef.GeneralReadMethodParameter(ma.Parameter1[k], nil, ETL.SMap))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("columnMeta", err)
		return
	}
	return
}
func (ma Meta) colMetaMap() (meta any, err error) {
	var ter ColumnEr
	if meta, err = parDef.CallMethod(ma.Drive, ma.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = ma.columnEr(v); err != nil {
			return
		}
		if result, err = ter.MetaMap(parDef.CtxBackground(parDef.GeneralReadMethodParameter(ma.Parameter1[k], nil, ""))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("colMetaMap", err)
		return
	}
	return
}
func ExternalCallLobColumnTypeFunc(s any, _ any) (res any, err error) {
	var p = make(map[string]string)
	var sp []map[string]any
	switch s.(type) {
	case []map[string]any:
		sp = s.([]map[string]any)
	}
	for _, vv := range sp {
		pp := ref.MapNullActive(vv)
		column, ok1 := pp["columnName"]
		colType, ok2 := pp["columnType"]
		colLength, ok3 := pp["dataLength"]
		if ok1 && ok2 {
			if !ok3 {
				p[fmt.Sprintf("%v", column)] = fmt.Sprintf("%v", colType)
			} else {
				p[fmt.Sprintf("%v", column)] = fmt.Sprintf("%v(%v)", colType, colLength)
			}
		}
	}
	if len(p) == 0 {
		return
	}
	return p, nil
}
func (ma Meta) lobC() (meta any, err error) {
	var ter ColumnEr
	if meta, err = parDef.CallMethod(ma.Drive, ma.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = ma.columnEr(v); err != nil {
			return
		}
		if result, err = ter.LobMeta(parDef.CtxBackground(parDef.GeneralReadMethodParameter(ma.Parameter1[k], ExternalCallLobColumnTypeFunc, ETL.SMap))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("lobC", err)
		return
	}
	return
}
func (ma Meta) bigC() (meta any, err error) {
	var ter ColumnEr
	if meta, err = parDef.CallMethod(ma.Drive, ma.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = ma.columnEr(v); err != nil {
			return
		}
		if result, err = ter.BigVarcharMeta(parDef.CtxBackground(parDef.GeneralReadMethodParameter(ma.Parameter1[k], ExternalCallLobColumnTypeFunc, ETL.SMap))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("bigC", err)
		return
	}
	return
}
func ExternalCallColumnCommentFunc(parameter any, _ any) (res any, err error) {
	var resMap = make(map[string]string)
	switch parameter.(type) {
	case []map[string]any:
		for _, vv := range parameter.([]map[string]any) {
			pp := ref.MapNullActive(vv)
			column, ok1 := pp["columnName"]
			comment, ok2 := pp["columnComment"]
			if ok1 && ok2 {
				resMap[fmt.Sprintf("%v", column)] = fmt.Sprintf("%v", comment)
			}
		}
	}
	res = resMap
	return
}
func (ma Meta) columnComment() (meta any, err error) {
	var ter ColumnEr
	if meta, err = parDef.CallMethod(ma.Drive, ma.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = ma.columnEr(v); err != nil {
			return
		}
		if result, err = ter.Comment(parDef.CtxBackground(parDef.GeneralReadMethodParameter(ma.Parameter1[k], ExternalCallColumnCommentFunc, ETL.SMap))); err != nil {
			return
		}
		fmt.Println("---------ddfff:", result.Sql, result.Result)
		return
	}); err != nil {
		err = ref.ErrAddPrintf("columnComment", err)
		return
	}
	return
}
func (ma Meta) columnAttributeMapClip() (meta any, err error) {
	var ter ColumnEr
	if meta, err = parDef.CallMethod(ma.Drive, ma.Join, func(k int, v string) (result global.Return, err error) {
		if ter, err = ma.columnEr(v); err != nil {
			return
		}
		if result, err = ter.ColumnAttributeMapClip(parDef.CtxBackground(parDef.GeneralReadMethodParameter(ma.Parameter1[k], nil, ""))); err != nil {
			return
		}
		return
	}); err != nil {
		err = ref.ErrAddPrintf("columnAttributeMapClip", err)
		return
	}
	return
}
func (ma Meta) GetColumnMetaST() (result any, err error) {
	return ma.columnMeta()
}
func (ma Meta) GetColMetaMapST() (result any, err error) {
	return ma.colMetaMap()
}
func (ma Meta) GetColAttributeMapST() (result any, err error) {
	return ma.columnAttributeMapClip()
}
func (ma Meta) GetLobColumnMetaS() (result any, err error) {
	return ma.lobC()
}
func (ma Meta) GetBigVarcharColumnMetaS() (result any, err error) {
	return ma.bigC()
}
